package com.voxel.sense.infomsg.module.task.list;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.utils.TimeUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.OnCustomItemClickListener;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.view.SwitchButton;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.response.TaskInfoEntity;
import cyy.example.com.facotry.share.SPEngine;

/**
 * @author ChenYiYao
 * @date 2018/12/13
 * des :
 */
public class TaskListAdapter extends RecyclerAdapter<TaskInfoEntity.ObBean> {
    private OnCustomItemClickListener mLocationClickListener;
    private OnCustomItemClickListener mSwitchClickEvnet;
    private OnCustomItemClickListener mdeleteClickListener;


    @Override
    protected int getItemViewType(int position, TaskInfoEntity.ObBean bean) {
        return R.layout.item_recycler_list_task;
    }

    @Override
    protected ViewHolder onCreateViewHolder(View root, int viewType) {
        return new Holder(root);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ((Holder) holder).sbtn_switch.setOnStateChangedListener(new SwitchButton.OnStateChangedListener() {
            @Override
            public void toggleToOn(View view) {
                if (mSwitchClickEvnet != null) {
                    mSwitchClickEvnet.onClick(view, mDataList.get(position), position);
                }
            }

            @Override
            public void toggleToOff(View view) {

            }
        });

        ((Holder) holder).iv_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLocationClickListener != null) {
                    mLocationClickListener.onClick(v, mDataList.get(position), position);
                }
            }
        });

        ((Holder) holder).iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mdeleteClickListener != null) {
                    mdeleteClickListener.onClick(v, mDataList.get(position), position);
                }
            }
        });
    }

    public void setLocationClickEvent(OnCustomItemClickListener clickEvent) {
        this.mLocationClickListener = clickEvent;
    }

    public void setSwitchClickEvent(OnCustomItemClickListener clickEvent) {
        this.mSwitchClickEvnet = clickEvent;

    }

    public void setDeleteClickListener(OnCustomItemClickListener mdeleteClickListener) {
        this.mdeleteClickListener = mdeleteClickListener;
    }

    public static class Holder extends ViewHolder<TaskInfoEntity.ObBean> {
        @BindView(R.id.tv_task_statu)
        TextView tv_task_statu;//任务状态
        @BindView(R.id.tv_task_name)
        TextView tv_task_name;//任务名称
        @BindView(R.id.tv_task_type)
        TextView tv_task_type;//任务类型
        @BindView(R.id.tv_task_target)
        TextView tv_task_target;
        @BindView(R.id.tv_task_content)
        TextView tv_task_content;
        @BindView(R.id.tv_start_time)
        TextView tv_start_time;
        @BindView(R.id.tv_end_time)
        TextView tv_end_time;
        @BindView(R.id.iv_location)
        ImageView iv_location;
        @BindView(R.id.iv_delete)
        ImageView iv_delete;
        @BindView(R.id.sbtn_switch)
        SwitchButton sbtn_switch;
        @BindView(R.id.ic_task_img)
        ImageView ic_task_img;


        public Holder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(TaskInfoEntity.ObBean bean) {

            /**
             *   1.当是待接收状态且是派发人身份时，显示删除按钮，隐藏接收按钮
             *   2.当是待接收状态且不是派发人时，显示接受按钮，隐藏按钮
             *   3.其它状态，接收 和删除都隐藏
             */
//            if (Constants.TASK_UNACCEPT_TYPE.equals(bean.getTaskStatu()) &&
//                    SPEngine.getSPEngine().getUserInfo().getUser_id().equals(bean.getFromId())) {
//                iv_delete.setVisibility(View.VISIBLE);
//                sbtn_switch.setVisibility(View.GONE);
//                iv_delete.setClickable(true);
//            } else if (Constants.TASK_UNACCEPT_TYPE.equals(bean.getTaskStatu())) {
//                sbtn_switch.setVisibility(View.VISIBLE);
//                iv_delete.setVisibility(View.GONE);
//                sbtn_switch.setOpened(false);
//            } else {
                iv_delete.setVisibility(View.VISIBLE);
                iv_delete.setClickable(false);
//                sbtn_switch.setVisibility(View.GONE);
//            }
            if(Constants.TASK_UNACCEPT_TYPE.equals(bean.getTaskStatu())){
                //待接收的情况
                sbtn_switch.setVisibility(View.VISIBLE);
                iv_delete.setVisibility(View.GONE);
                sbtn_switch.setOpened(false);
            }else if(Constants.TASK_CHECK_NOT_PASS.equals(bean.getTaskStatu())){
                //未通过
                sbtn_switch.setVisibility(View.INVISIBLE);
                iv_delete.setVisibility(View.VISIBLE);
            }else if(Constants.TASK_ACTION_TYPE.equals(bean.getTaskStatu())){
                //执行中
                sbtn_switch.setVisibility(View.INVISIBLE);
                String user_id = SPEngine.getSPEngine().getUserInfo().getUser_id();
                if (user_id.equals(bean.getFromId())) {
                    iv_delete.setVisibility(View.VISIBLE);
                }else{
                    iv_delete.setVisibility(View.INVISIBLE);
                }
            }if (Constants.TASK_CHECK_PASS.equals(bean.getTaskStatu())){
                sbtn_switch.setVisibility(View.INVISIBLE);
                iv_delete.setVisibility(View.INVISIBLE);
            }

            tv_task_type.setText(bean.getTaskTypeDesc());
            //设置任务状态
            tv_task_statu.setTextColor(getTaskStatuDescColor(bean.getTaskStatu()));
            tv_task_statu.setText(getTaskStatuDesc(bean.getTaskStatu()));

            tv_task_name.setText(bean.getTaskName());
            tv_task_content.setText(bean.getTaskContents());
            tv_start_time.setText(bean.getBeginTime());
            tv_task_target.setText(bean.getCompanyName());

            GlideUtils.GlideLoadPic(ic_task_img.getContext(), R.mipmap.a_level_firm, ic_task_img);
            String endTime = bean.getEndTime();
            String nowString = TimeUtils.getNowString(new SimpleDateFormat(Constants.TimeYMDFormat));
            //任务过期或是结束为当天的则标红显示
            if (nowString.compareTo(endTime) >= 0) {
                tv_end_time.setTextColor(Factory.app().getResources().getColor(R.color.red_600));
            } else {
                tv_end_time.setTextColor(Factory.app().getResources().getColor(R.color.textDark));
            }
            tv_end_time.setText(bean.getEndTime());

        }


        /**
         * TODO: 被驳回和未通过状态不明确。
         *
         * @param statu 任务状态（0待接收/1执行中/2待审核/4已结案/5未通过/6任务失败）
         */
        private String getTaskStatuDesc(String statu) {
            String desc = "";
            if (Constants.TASK_UNACCEPT_TYPE.equals(statu)) {
                desc = "待接收";
            } else if (Constants.TASK_ACTION_TYPE.equals(statu)) {
                desc = "执行中";

            } else if (Constants.TASK_FINISH_TYPE.equals(statu)) {
                desc = "待审核";

            } else if (Constants.TASK_CHECK_NOT_PASS.equals(statu)) {
                desc = "未通过";

            } else if (Constants.TASK_CHECK_PASS.equals(statu)) {
                desc = "已完成";

            } else if (Constants.TASK_CHECK_OVERDUE.equals(statu)) {
                desc = "任务失败";
            }
            return desc;
        }

        /**
         * TODO: 被驳回和未通过状态不明确。
         *
         * @param statu 任务状态（0待接收/1执行中/2待审核/4已结案/5未通过/6任务失败）
         */
        private int getTaskStatuDescColor(String statu) {


            int color = R.color.black;
            if (Constants.TASK_UNACCEPT_TYPE.equals(statu)) {
                color = R.color.green_600;
            } else if (Constants.TASK_ACTION_TYPE.equals(statu)) {
                color = R.color.blue_600;

            } else if (Constants.TASK_FINISH_TYPE.equals(statu)) {
                color = R.color.blue_600;

            } else if (Constants.TASK_CHECK_NOT_PASS.equals(statu)) {
                color = R.color.red_600;

            } else if (Constants.TASK_CHECK_PASS.equals(statu)) {
                color = R.color.yellow_900;

            } else if (Constants.TASK_CHECK_NOT_PASS.equals(statu)) {
                color = R.color.red_600;
            }
            return Factory.app().getResources().getColor(color);
        }
    }

}



